clear all 
set more off

*****************************************************************************
******************** SECTOR LEVEL REGRESSIONS *******************************
*****************************************************************************

// Load data created in setup_data_4.R 
use "..\data\sectors_unbalanced.dta", clear 

// Create and format date variable 
gen date = yq(year, quarter) 
format date %tq

// Create numerical firm identifier
egen sector_id = group(naics_code)
tsset sector_id date

// Select top K and Q firms based on lifetime obligations 
preserve 
tempfile sectorranking
collapse (sum) sector_quarter_obligations, by(naics_code sector_id)
egen sector_rank = rank(-sector_quarter_obligations) 

local K 10
local Q 1000

gen top_K = sector_rank <= `K'
gen top_Q = sector_rank <= `Q' 

keep sector_id sector_rank top_K top_Q
save `sectorranking' 

restore 

merge m:1 sector_id using `sectorranking'

// Fill panel so it is balanced
tsfill
keep sector_id date sector_quarter_obligations top_K top_Q
replace sector_quarter_obligations = 0 if sector_quarter_obligations == .

// Generate 4-quarter lag obligations variable and 4-quarter growth rate
gen log_obligations = log(sector_quarter_obligations) 
gen lag_obligations = L4.log_obligations 
gen z_it            = log_obligations - lag_obligations 

// Calculate aggregate spending in each quarter
bys date: egen aggregate_G = sum(sector_quarter_obligations)


// Calculate average growth rate of top 1000 sectors
bys date: egen zbar_temp = mean(z_it) if top_Q == 1
bys date: egen zbar_t = max(zbar_temp) 

// Calculate spending share for top k sectors
sort sector_id date 
gen g_share = L4.sector_quarter_obligations/L4.aggregate_G if top_K == 1
gen z_diff  = z_it - zbar_t if top_K == 1
gen gamma_t = g_share*z_diff if top_K == 1

// Collapse data 
keep if top_K == 1

collapse (mean) aggregate_G (sum) gamma_t, by(date) 

tsset date

gen log_G   = log(aggregate_G) 
gen delta_G = log_G - L4.log_G 

label var gamma_t "$\Gamma_t$"
label var delta_G "$\Delta G$ (Sectors)"


eststo clear 
eststo: reg delta_G gamma_t if date > yq(2001,4) & date < yq(2020,1) 
eststo: reg delta_G gamma_t L.gamma_t L2.gamma_t if date > yq(2001,4) & date < yq(2020,1) 

*****************************************************************************
******************** FIRM LEVEL REGRESSIONS *********************************
*****************************************************************************

// Load data created in setup_data_4.R 
use "..\data\firms_unbalanced.dta", clear 

// Create and format date variable 
gen date = yq(year, quarter) 
format date %tq

// Create numerical firm identifier
egen firm_id = group(recipient_parent_duns)
tsset firm_id date

// Select top K and Q firms based on lifetime obligations 
preserve 
tempfile ranking
collapse (sum) firm_quarter_obligations, by(recipient_parent_duns firm_id) 
egen firm_rank = rank(-firm_quarter_obligations) if recipient_parent_duns != "NA" 

local K 10
local Q 1000

gen top_K = firm_rank <= `K'
gen top_Q = firm_rank <= `Q' 

keep firm_id firm_rank top_K top_Q
save `ranking' 

restore 

merge m:1 firm_id using `ranking'

// Fill panel so it is balanced
tsfill
keep firm_id date firm_quarter_obligations top_K top_Q
replace firm_quarter_obligations = 0 if firm_quarter_obligations == .

// Generate 4-quarter lag obligations variable and 4-quarter growth rate
gen log_obligations = log(firm_quarter_obligations) 
gen lag_obligations = L4.log_obligations 
gen z_it            = log_obligations - lag_obligations 

// Calculate aggregate spending in each quarter
bys date: egen aggregate_G = sum(firm_quarter_obligations)


// Calculate average growth rate of top 1000 firms
bys date: egen zbar_temp = mean(z_it) if top_Q == 1
bys date: egen zbar_t = max(zbar_temp) 

// Calculate spending share for top k firms
sort firm_id date 
gen g_share = L4.firm_quarter_obligations/L4.aggregate_G if top_K == 1
gen z_diff  = z_it - zbar_t if top_K == 1
gen gamma_t = g_share*z_diff if top_K == 1

// Collapse data 
keep if top_K == 1

collapse (mean) aggregate_G (sum) gamma_t, by(date) 

tsset date

gen log_G   = log(aggregate_G) 
gen delta_G = log_G - L4.log_G 

label var gamma_t "$\Gamma_t$"
label var delta_G "$\Delta G$ (Firms)"

eststo: reg delta_G gamma_t if date >= yq(2001,2) & date < yq(2020,1) 
eststo: reg delta_G gamma_t L.gamma_t L2.gamma_t if date >= yq(2001,4) & date < yq(2020,1) 

esttab using "../output/Table2_upper.tex", label se r2 wide substitute(_\_) nogaps nocons replace mtitle("\shortstack{$\Delta G$ \\ (Sectors)}" "\shortstack{$\Delta G$ \\ (Sectors)}" "\shortstack{$\Delta G$ \\ (Firms)}" "\shortstack{$\Delta G$ \\ (Firms)}")